package com.anyonavinfo.cpaddemo.view.mainview.miniweather;

import android.os.Handler;

import com.anyonavinfo.cpaddemo.data.hostinfo.source.service.E3HWServiceHelper;
import com.anyonavinfo.cpaddemo.data.locationinfo.LocationInfo;
import com.anyonavinfo.cpaddemo.data.locationinfo.source.LocationInfoDataSource;
import com.anyonavinfo.cpaddemo.data.locationinfo.source.LocationInfoRepository;
import com.anyonavinfo.cpaddemo.data.timeinfo.TimeInfo;
import com.anyonavinfo.cpaddemo.data.timeinfo.source.TimeInfoDataSource;
import com.anyonavinfo.cpaddemo.data.timeinfo.source.TimeInfoRepository;
import com.anyonavinfo.cpaddemo.data.weatherinfo.WeatherInfo;
import com.anyonavinfo.cpaddemo.data.weatherinfo.source.WeatherInfoDataSource;
import com.anyonavinfo.cpaddemo.data.weatherinfo.source.WeatherInfoRepository;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by ZuiC on 2017/1/16.
 */

public class MiniWeatherPresenter implements MiniWeatherContract.Presenter, E3HWServiceHelper.ServiceObserver{

    private static final String TAG = "MiniWeatherPresenter";
    private E3HWServiceHelper serviceHelper;
    private MiniWeatherContract.View mView;
    private TimeInfoRepository mTimeInfoRepository;
    private WeatherInfoRepository mWeatherInfoRepository;
    private static final int TIME_REFRESH_DELAY = 3000;
    private final Handler mHandler = new Handler();




    public MiniWeatherPresenter(MiniWeatherContract.View view, TimeInfoRepository timeInfoRepository
    , WeatherInfoRepository weatherInfoRepository){
        mView = view;
        mView.setPresenter(this);
        mTimeInfoRepository = timeInfoRepository;
        mWeatherInfoRepository = weatherInfoRepository;
        serviceHelper = E3HWServiceHelper.getServiceHelperInstance();
        serviceHelper.registerObserver(this);
    }


    @Override
    public void start() {
        mHandler.post(mTimeRefresher);
<<<<<<< HEAD
        mWeatherInfoRepository.getLocalWeatherInfo(new WeatherInfoDataSource.GetLocalWeatherInfoCallBack() {
            @Override
            public void onGetLocalWeatherInfo(WeatherInfo weatherInfo) {
                String temp = weatherInfo.getCurTemperature();
                String type = weatherInfo.getCurWeatherType();
                if (!temp.equals("NULL") && type.equals("NULL")){
                    mView.updateWeatherType(type);
                    mView.updateTemperature(temp + "°");
                }
            }
        });
=======
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    }


    private final Runnable mTimeRefresher = new Runnable() {
        @Override
        public void run() {
            mTimeInfoRepository.getTimeInfo(new TimeInfoDataSource.GetTimeInfoCallBack() {
                @Override
                public void onGetTimeInfo(TimeInfo timeInfo) {
                    mView.updateDate(timeInfo.getDate());
                    mView.updateTime(timeInfo.getTime());
                }
            });
            mHandler.postDelayed(this, TIME_REFRESH_DELAY);
        }
    };

    @Override
    public void update(String json) {
        JSONObject jsonparas;
        JSONObject jsonobject;
        double latitude, longitude;
        try {
            jsonobject = new JSONObject(json.toString());
            String JsonID = jsonobject.names().getString(0);
            if(JsonID.endsWith("GPSINFO_INSTANT")) {
                jsonparas = new JSONObject(
                        jsonobject.getString("GPSINFO_INSTANT"));
                latitude = Double.valueOf(jsonparas
                        .getString("latitude"));
                longitude = Double.valueOf(jsonparas
                        .getString("longitude"));
                if (longitude != 0 && latitude != 0) {
                    LocationInfoRepository.getInstance().getLocationInfo(latitude, longitude, new LocationInfoDataSource.GetLocationInfoCallBack() {
                        @Override
                        public void onGetLocationInfo(LocationInfo locationInfo) {
                            mWeatherInfoRepository.getWeatherInfo(locationInfo.getCurCity(), new WeatherInfoDataSource.GetWeatherInfoCallBack() {
                                @Override
                                public void onGetWeatherInfo(WeatherInfo weatherInfo) {
                                    mView.updateWeatherType(weatherInfo.getCurWeatherType());
                                    mView.updateTemperature(weatherInfo.getCurTemperature());
                                }
                            });
                        }
                    });
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
